<template>
  <div class="login">
    <div class="register"></div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">生产管理系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" id="catcha-img">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
          @change="handleChange"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
          <span slot="append" class="error-tips">
            <i class="el-inpit__icon el-icon-error" v-if="errorTip"></i>
            <i class="el-inpit__icon el-icon-success" v-if="successTip"></i>
          </span>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <div class="flex_row">
        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin: 0px 0px 25px 0px"
          >记住密码</el-checkbox
        >
      </div>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <!-- <span>Copyright © 2018-2021 xlpx.org.cn All Rights Reserved.</span> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg, loginCode } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      errorTip: false,
      successTip: false,
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
    "loginForm.code"(val, oldval) {
      let _this = this;
      // console.log(val, oldval);
      // isCodeTrue 0-错误；1-正确
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
    const app = document.getElementById("app");
    app.style.transform = "none";
    app.style.width = "100%";
    app.style.height = "auto";
    app.style.left = "0";
  },
  methods: {
    forgetPsw() {
      this.$router.push({ path: "/forgetPsw" });
    },
    handleChange(code) {
      // var _this = this;
      // let uuid = this.loginForm.uuid;
      // const param = {
      //   code,
      //   uuid
      // };
      // loginCode(param)
      //   .then(res => {
      //     console.log(res);
      //     if (res.code == "200") {
      //       if (res.isCodeTrue == "1") {
      //         _this.successTip = true;
      //         _this.errorTip = false;
      //       } else if (res.isCodeTrue == "0") {
      //         _this.errorTip = true;
      //         _this.successTip = false;
      //       } else if (res.isCodeTrue == "-1") {
      //         _this.successTip = false;
      //         _this.errorTip = false;
      //         _this.$message({ message: "验证码超时", type: "error" });
      //         _this.loginForm.code = "";
      //         this.getCode();
      //       }
      //     } else {
      //       _this.successTip = false;
      //       _this.errorTip = false;
      //       _this.$message({ message: res.msg, type: "error" });
      //     }
      //   })
      //   .catch(error => {
      //     console.log(error);
      //   });
    },
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              const isMobile = window.matchMedia("(max-width: 768px)").matches;
              const defaultPath = isMobile ? "/mobile/repair" : "/";
              this.$router.push({ path: defaultPath }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  height: 100%;
}
.title {
  margin: 0px auto 28px auto;
  text-align: center;
  color: #409eff;
  font-size: 30px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 480px;
  padding: 56px 48px 35px 48px;
  position: absolute;
  right: 10%;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .login {
    justify-content: center;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: calc(90%);
    // margin-left: 26px;
    // padding: 56px 48px 35px 48px;
    // padding: 0 26px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    // right: 10%;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .title {
    font-size: 24px;
    margin-bottom: 20px;
  }

  .login-code {
    width: 40%;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #0770b4;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
.flex_row {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #8492a6;
}
.register {
  width: 100%;
  height: 100vh;
  background-image: url("../assets/images/bg2.png");
}
.register img {
  // width: 100%;
  // height: calc(100vh - 4px);
  // margin-right: 90px;
}
.title img {
  width: 308px;
  height: 52px;
}
</style>
<style scoped>
/deep/ #catcha-img .el-input-group__append {
  padding: 0;
  /* height:40px; */
  background: #fff;
  border: 0;
  position: absolute;
  right: 15px;
  top: -1px;
}
.error-tips {
  position: absolute;
  right: 10px;
  top: 12px;
}
.error-tips i {
  color: #f56c6c;
}
/deep/ #catcha-img .el-input-group__append img {
  height: 40px;
  width: 100px;
}
.error-tips .el-icon-success {
  color: #53ad41;
}
.error-tips .el-icon-error {
  color: #f56c6c;
}
</style>